﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace KoolearnSample.Model
{
    public class RelayCommand : ICommand
    {
        #region Members
        readonly Func<Boolean> _canExecute;
        readonly Action _execute;
        #endregion

        #region Constructors

        public void Execute(object parameter)
        {
            _execute();
        }

        public RelayCommand(Action execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action execute, Func<Boolean> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
            _execute = execute;
            _canExecute = canExecute;
        }

        #endregion

        #region ICommand Members
        public event EventHandler CanExecuteChanged;
        public void OnCanExecuteChanged()
        {
            EventHandler handler = CanExecuteChanged;
            if (handler != null)
            {
                handler(this, null);
            }
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }
        #endregion
    }
}
